const banner = require( 'express' );


var route = banner.Router(),
    common = require( '../../lib/common' );


route.get( '/', (req,res)=>{

    //查询banner
    common.db().query( `select * from banner`,(err,data)=>{
        if(err){
            console.log( err );
            res.status(500).send( '数据库错误' ).end();
        }else{
            res.render( 'admin/banner/index.ejs',{list:data} );
        }
    } );

} );


route.get( '/add',(req,res)=>{
    res.render( 'admin/banner/add.ejs' );
} );


route.post( '/add',(req,res)=>{
    var href = common.uploder(req,res);
    href = JSON.stringify( href );
    
    //添加数据
    common.db().query( `insert into banner(title,description,href) values('${req.body.title}','${req.body.description}','${href}')`,(err)=>{
        if(err ){
            console.log( err );
        }else{
            res.redirect( '/admin/banner' );
        }
    } );
} );

route.get( '/edit',(req,res)=>{
    res.render( 'admin/banner/edit.ejs' );
} );

module.exports = route;